<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="ar.edu.itba.bundundu.domain.CalendarSharing" %>
<%@ page import="ar.edu.itba.bundundu.domain.CalendarItem"%>
<%@ page import="ar.edu.itba.bundundu.domain.FeedItem"%>
<%@ page import="ar.edu.itba.bundundu.dao.UserDao" %>
<%@ page import="ar.edu.itba.bundundu.domain.UserIds" %>



<% request.setAttribute("tab", 6); %>
<%@ include file="/header.jsp" %>

<script>
<!--
function publish( ){
	//TODO: levantar el nombre del calendario y el friend y para mostrarlo en el mensaje
	var calendar = "<%=request.getAttribute("calendar_name")%>";
	var friend = "<%=request.getAttribute("calendar_friend")%>";
	var attachment = {'media':[{'type':'image','src':'http://bundundu.appspot.com/images/bundundu.JPG','href':'http://apps.facebook.com/bundundu/'}]};
 	var user_message = 'I shared my ' + calendar +' calendar with you using Bundundu!';
 var id = Facebook.streamPublish(user_message, attachment, null, friend);
}


//-->
</script>
<%
String error = (String)request.getAttribute("error");
String success = (String)request.getAttribute("success");

if(error !=null){
  %>
  <fb:error><fb:message>Error!</fb:message>
  	<%=error %>
  	</fb:error>
  	<%
}else if(success !=null){
  
  %>
<script>
<!--
document.getElementById('success').addEventListener('onload',publish());
//-->
</script>
  
  
  <div id="success" >
  <fb:success><fb:message>Great!</fb:message>
  <%=success%>
  </fb:success>
  </div>
    
  <%
  
}

%>



<style type="text/css">

#sharing {
       border-style: solid none solid none;
       border-color: #5872A7;
}
</style>



<table id="sharing" width="100%" rules="rows">
<tr>
<th> Calendar</th>
<th> Friend </th>
<th> Permission Settings </th>
<th> Remove </th>
</tr>
<%
	CalendarSharing calendar = (CalendarSharing)request.getAttribute("calendars");
	Long uid = (Long)request.getAttribute("uid");
	String exclude_uids = (String)request.getAttribute("uids");
	for(int i=0; i< calendar.getSize(); i++){
	  CalendarItem item = calendar.get(i);
	  %>
	  	<tr>
	  		
	  		
		  	<td rowspan="<%=(item.getSize()+1) %>">
		  		<%= item.name%>
		  		
		  	</td>
			
			<td colspan="3">
				<form  name="addaccess" action="<%=url%>addaccess" method="post">
				<input type="hidden" name="calendar_url" value="<%= item.url %>"/>
				<input type="hidden" name="calendar_name" value="<%=item.name %>"/>
				<fb:friend-selector uid="<%=uid %>" name="uid" idname="friend_sel" exclude_ids="<%=exclude_uids %>" />
			
				<select name="level">
					<option value="1">Make changes AND manage sharing</option>
					<option value="2">Make changes to events</option>
					<option value="3">See all event details</option>
					<option value="4">See only free/busy (hide details)</option>
				</select>
	            <input type="submit" name="Add!" value="Add!"/>
	            </form>
			</td>	
		</tr>
		<%
		for(int j=0; j< item.getSize(); j++){
		  FeedItem feed = item.get(j);
		  UserIds friend = UserDao.getUserByEmail(feed.email);
		  if( friend == null){
		    %>
		  	<tr>
		  		<td> <%=feed.email %> </td>
		  		<td><%=feed.getLevelPlainText() %></td>
		  		<td> 
		  			<form  name="delete" action="<%=url%>deleteaccess" method="post">
					  	<input type="hidden" name="acl_id" value="<%=feed.aclentry.getId().toString() %>"/>
					  	<input type="hidden" name="url" value="<%=item.url %>"/>
					  	<input type="image" src="http://bundundu.appspot.com/images/x.JPG"alt="Remove" width="20"/>
				  	</form>
  				</td>
		  	</tr>
		  	<%
		  }else {
		  %>
		  	<tr>
		  		<td> <fb:name uid="<%=friend.getUid() %>" capitalize="true" /> </td>
		  		<td><%=feed.getLevelPlainText() %></td>
		  		<td> <%if(! uid.equals(friend.getUid()) &&  ! feed.email.equals(UserDao.getUser(uid).getEmail())    ){%>
		  			<form  name="delete" action="<%=url%>deleteaccess" method="post">
					  	<input type="hidden" name="acl_id" value="<%=feed.aclentry.getId().toString() %>"/>
					  	<input type="hidden" name="url" value="<%=item.url %>"/>
					  	<input type="image" src="http://bundundu.appspot.com/images/x.JPG"alt="Remove" width="20"/>
				  	</form>
				  	<%} %>
  				</td>
		  	</tr>
		  	<%
		  }
		}
	}
%>
</table>


	